﻿using System;
using System.Collections;
using System.Configuration;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Web_Oto.Business;
using Web_Oto.Common;

namespace Web_Oto.Administer
{
    public partial class Banner : System.Web.UI.Page
    {
        private BannerBus _business;
        private string _imagePath = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            _business = new BannerBus();
            _imagePath = ConfigurationManager.AppSettings["bannerPath"];
        }

        protected void grid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            var newsId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"].ToString();
            int id;
            int.TryParse(newsId, out id);

            var fileName = _business.GetFilePathById(id);
            CommonHelper.DeleteFile(_imagePath, fileName);

            var result = _business.Delete(id);
            if (result > 0)
            {
                msgBox.InnerHtml = "<p><strong>Xóa thành công</strong></p>";
                msgBox.Attributes["class"] = "msg msg-ok";
            }
            else
            {
                msgBox.InnerHtml = "<p><strong>Xóa không thành công</strong></p>";
                msgBox.Attributes["class"] = "msg msg-error";
            }
        }

        protected void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            grid.DataSource = _business.GetAllBanners();
        }

        protected void grid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            var gdItem = (e.Item as GridEditFormItem);
            var editableItem = ((GridEditableItem)e.Item);
            var values = new Hashtable();
            editableItem.ExtractValues(values);

            var banner = new Data.Banner
                             {
                                 Id = (int) editableItem.GetDataKeyValue("Id")
                             };

            if (gdItem != null)
            {
                var uploader = gdItem.FindControl("uploadAvatar") as RadUpload;
                var hdfImage = ((HiddenField)gdItem.FindControl("hdfImage"));
                var txtIntro = gdItem.FindControl("txtIntro") as RadTextBox;
                var txtUrl = gdItem.FindControl("txtUrl") as RadTextBox;

                if (uploader != null && hdfImage != null && txtIntro != null && txtUrl != null)
                {
                    if (uploader.UploadedFiles.Count > 0)
                    {
                        var oldPathImage = hdfImage.Value;
                        var avatar = uploader.UploadedFiles[0].FileName;
                        var filePath = CommonHelper.GetFilePath(_imagePath, ref avatar, oldPathImage);
                        uploader.UploadedFiles[0].SaveAs(Server.MapPath(filePath));
                        //banner.Image = filePath;
                        banner.Image = avatar;
                    }

                    banner.Url = txtUrl.Text;
                    banner.Intro = txtIntro.Text;
                    var result = _business.Update(banner);
                    if (result > 0)
                    {
                        msgBox.InnerHtml = "<p><strong>Cập nhật thành công</strong></p>";
                        msgBox.Attributes["class"] = "msg msg-ok";
                    }
                    else
                    {
                        msgBox.InnerHtml = "<p><strong>Cập nhật không thành công</strong></p>";
                        msgBox.Attributes["class"] = "msg msg-error";
                    }
                }
            }
        }

        protected void grid_InsertCommand(object sender, GridCommandEventArgs e)
        {
            var gdItem = (e.Item as GridEditFormItem);
            var editableItem = ((GridEditableItem)e.Item);
            var values = new Hashtable();
            editableItem.ExtractValues(values);

            var banner = new Data.Banner();

            if (gdItem != null)
            {
                var uploader = gdItem.FindControl("uploadAvatar") as RadUpload;
                var txtIntro = gdItem.FindControl("txtIntro") as RadTextBox;
                var txtUrl = gdItem.FindControl("txtUrl") as RadTextBox;

                if (uploader != null && txtIntro != null && txtUrl != null)
                {
                    if (uploader.UploadedFiles.Count > 0)
                    {
                        var avatar = uploader.UploadedFiles[0].FileName;
                        var filePath = CommonHelper.GetFilePath(_imagePath, ref avatar);
                        uploader.UploadedFiles[0].SaveAs(Server.MapPath(filePath));
                        //banner.Image = filePath;
                        banner.Image = avatar;
                    }
                    else
                    {
                        msgBox.InnerHtml = "<p><strong>Vui lòng chọn ảnh cho banner</strong></p>";
                        msgBox.Attributes["class"] = "msg msg-error";
                        return;
                    }

                    banner.Url = txtUrl.Text;
                    banner.Intro = txtIntro.Text;
                    var result = _business.Insert(banner);
                    if (result > 0)
                    {
                        msgBox.InnerHtml = "<p><strong>Thêm mới thành công</strong></p>";
                        msgBox.Attributes["class"] = "msg msg-ok";
                    }
                    else
                    {
                        msgBox.InnerHtml = "<p><strong>Thêm mới không thành công</strong></p>";
                        msgBox.Attributes["class"] = "msg msg-error";
                    }
                }
            }
        }
    }
}